﻿
using Newtonsoft.Json;
using RestSharp;
using ShiBieProject;
using System;
using System.Collections.Generic;

namespace TaxAssistantBase
{
    public static class RestTool2
    {

     private static string apiBaseUrl = "http://mini.hongfund.com/";
       private static string apiBaseUrlA = "http://localhost:8080/";

        private static WsdLogger wlog = new WsdLogger("GuanLiLog");
        public static RestClient GetRestClient()
        {
            RestClient client;

            client = new RestClient(apiBaseUrl);


            return client;
        }
        public static RestClient GetRestClientA()
        {
            RestClient client;

            client = new RestClient(apiBaseUrlA);


            return client;
        }

        /// <summary>
        /// post
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="restType"></param>
        /// <param name="resource"></param>
        /// <param name="jsonBody"></param>
        /// <returns></returns>
        public static T PostA<T>(string resource, InvoiiceOcrModel jsonBody) where T : RestResultModel1, new()
        {
            try
            {
                var client = GetRestClient();

                var request = new RestRequest(resource, Method.POST);
                //从配置中读取token,添加到header中
                request.AddHeader("token", "");

                //增加请求对象

                request.AddJsonBody(jsonBody);
                wlog.Write("转换数据");
                IRestResponse response2 = client.Execute(request);
               
                //LogTool.WriteLog("resource:" + resource + ",jsonBody:" + jsonBody.ToString(), "post.txt");
                JsonSerializerSettings jsetting = new JsonSerializerSettings();
                jsetting.NullValueHandling = NullValueHandling.Ignore;
                wlog.Write("转换数据结束");
                var result = JsonConvert.DeserializeObject<T>(response2.Content, jsetting);
             //   var result = JsonConvert.DeserializeObject<List<InvoiiceOcrModel>>(response2.Content);
                if (result == null)
                {
                    //LogTool.WriteException("RestTool.Post Post结果为null(" + client.BaseUrl + resource + ")");

                    var resultModel = new T();
                    resultModel.errorCode = CloudResultStateEnum.ApiNoAccess;
                    resultModel.errorMessage = "Post结果为null(" + client.BaseUrl + resource + ")";

                    return resultModel;
                }
                else if (!string.IsNullOrEmpty(result.status) || !string.IsNullOrEmpty(result.code))
                {
                    var resultModel = new T();
                    resultModel.errorCode = CloudResultStateEnum.ApiNoAccess;
                    resultModel.errorMessage = "Post结果为页面无法访问(" + client.BaseUrl + resource + "," + result.status + "," + result.code + ")";

                    //LogTool.WriteException("RestTool.Post Post结果为页面无法访问(" + client.BaseUrl + resource + "," + result.status + "," + result.code + ")");

                    return resultModel;
                }
                else if (result.errorCode == CloudResultStateEnum.TOKEN_INVALID)
                {
                    //LogTool.WriteException("token失效(" + client.BaseUrl + resource + ")" + result.errorMessage);

                    //var resultModel = UserTool.SetToken<T>(resource);
                    //if (resultModel != null)
                    //{
                    //    return resultModel as T;
                    //}

                    ////再次提交
                    ////LogTool.WriteException("RestTool.Post 获取token失效后重新获取token，再次提交post(" + client.BaseUrl + resource + "," + jsonBody + ") ");
                    //return Post<T>(resource, jsonBody);
                }
                else if (result.errorCode != CloudResultStateEnum.SUCCESS)
                {
                    //LogTool.WriteException("RestTool.Post Post结果未成功(" + client.BaseUrl + resource + ")" + result.errorCode);
                }
                else if (result.errorCode == CloudResultStateEnum.SUCCESS)
                {
                    String[] res = response2.Content.Split(',');
                    result.errorMessage = res[0];

                    return result;
                }
                return result;
            }
            catch (Exception ex)
            {
                //LogTool.WriteException("RestTool.Post", ex);

                var resultModel = new T();
                resultModel.errorCode = CloudResultStateEnum.NetTryEx;
                resultModel.errorMessage = ex.Message;

                //转换成子类
                return resultModel;
            }

        }


        /// <summary>
        /// post
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="restType"></param>
        /// <param name="resource"></param>
        /// <param name="jsonBody"></param>
        /// <returns></returns>
        public static T postGetInvoice<T>(string resource, InvoiiceOcrModel jsonBody) where T : RestResultInvoiceModel, new()
        {
            try
            {
                var client = GetRestClient();

                var request = new RestRequest(resource, Method.POST);
                //从配置中读取token,添加到header中
                request.AddHeader("token", "");

                //增加请求对象

                request.AddJsonBody(jsonBody);
                wlog.Write("转换数据");
                IRestResponse response2 = client.Execute(request);
                //LogTool.WriteLog("resource:" + resource + ",jsonBody:" + jsonBody.ToString(), "post.txt");
                JsonSerializerSettings jsetting = new JsonSerializerSettings();
                jsetting.NullValueHandling = NullValueHandling.Ignore;
                wlog.Write("转换数据结束");
                // var result = JsonConvert.DeserializeObject<T>(response2.Content, jsetting);
                var result = JsonConvert.DeserializeObject<T>(response2.Content, jsetting);
                if (result == null)
                {
                    //LogTool.WriteException("RestTool.Post Post结果为null(" + client.BaseUrl + resource + ")");
                    var resultModel = new T(); 
                    resultModel.errorCode = CloudResultStateEnum.ApiNoAccess;
                    resultModel.errorMessage = "Post结果为null(" + client.BaseUrl + resource + ")";

                    return resultModel;

                }
                return result;
            }
            catch (Exception ex)
            {
                //LogTool.WriteException("RestTool.Post", ex);

                var resultModel = new T();
                resultModel.errorCode = CloudResultStateEnum.NetTryEx;
                resultModel.errorMessage = ex.Message;

                //转换成子类
                return resultModel;
            }

        }

    }
}
